/*
 * Copyright (c) 2010, Ivan Latysh or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */

package com.addthis.classes {
import com.addthis.components.DAO;

import flash.display.Bitmap;
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.system.LoaderContext;

/**
 * Class represent a single sharing service with it's attributes  
 *
 * @author Ivan Latysh <ivanlatysh at gmail dot com>
 */
[Bindable]
public class Service {

  public var code:String;
  public var name:String;
  public var icon_url:String;
  public var icon:Bitmap;

  public function Service() {
  }

  /**
   * Load icon, which URL is in icon_url property
   */
  public function loadIcon():void {
    if (null==icon_url) return;

    // create a new context
    var _context:LoaderContext = new LoaderContext();
    _context.checkPolicyFile = true;

    // create a new loader
    var _loader:Loader = new Loader();
    // attach listeners
    _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
    _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderError);
    // load icon
    _loader.load(new URLRequest(icon_url), _context);
  }

  /**
   * Called when bitmap has been loader
   *
   * @param event result event
   */
  private function loaderComplete(event:Event):void {
    // get loader
    var _loader:Loader = Loader(event.target.loader);
    // detach listeners
    _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderComplete);
    _loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loaderComplete);
    // create a new bitmap image
    this.icon = Bitmap(_loader.content);
    // a hack, if it is a `More...` icon, save it in the DAO
    if (DAO.SERVICE_CODE_MORE == code) DAO.getInstance().addThisIcon = this.icon; 
  }

  /**
   * Called when bitmap has been loader
   *
   * @param event result event
   */
  private function loaderError(event:Event):void {
    // get loader
    var _loader:Loader = Loader(event.target.loader);
    // detach listeners
    _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderComplete);
    _loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loaderComplete);
    // set icon to null, so no image is displayed
    icon = null;
  }

  public function toString():String {
    return "Service{code=" + String(code) + ",name=" + String(name) + ",icon_url=" + String(icon_url) + ",icon=" + String(icon) + "}";
  }
}
}